/**
 * 定义一些全局变量
 */
var del_a_id = null; // 定义将要删除的上传文件 ID 临时寄存器
var new_uploaded_files = new Array(); // 定义新上传文件ID的缓存池，以免重复计算时长

(function () {
    /**
     * 上传附件
     * 使用延时加载的方式来解决js文件加载混乱而导致无法执行上传动作的问题
     */
    setTimeout('uploadFiles()', 1500);


})(jQuery);


/**
 * 从附件列表中删除一个附件
 */
function delAttachment(obj_btn_del) {
    /* 基础数据准备 */
    var obj_tr = obj_btn_del.parent().parent();
    if (obj_tr.prop("tagName") == 'TD') {
        obj_tr = obj_tr.parent();
    }
    var obj_gallery = obj_tr.parent().parent().parent();
    var obj_card = obj_gallery.parent().parent();
    var delete_type = obj_btn_del.data('delete-type');
    var duration = obj_btn_del.data('duration');
    var delete_id = obj_tr.attr('attachment_id');

    /* 处理附件 ID */
    var obj_attachment_ids = obj_card.find("input[name='attachments_" + delete_type + "[]']:last");
    var attachment_ids_new = obj_attachment_ids.val().replace(delete_id + ',', '');
    attachment_ids_new = attachment_ids_new.replace(',' + delete_id, '');
    attachment_ids_new = attachment_ids_new.replace(delete_id, '');
    obj_attachment_ids.val(attachment_ids_new);

    /* 处理附件数 */
    var count_attachment_now = obj_gallery.find('.count-attachment').text();
    obj_gallery.find('.count-attachment').text(count_attachment_now - 1);

    /* 处理时长 */
    if (delete_type == 'audio' || delete_type == 'video') {
        var obj_duration = obj_card.find("input[name='duration_" + delete_type + "[]']:last");
        var count_duration = obj_duration.val() - duration;
        obj_duration.val(count_duration);
        obj_gallery.find('.count-duration').text(count_duration);
    }

    /* 移除行 */
    obj_tr.remove();
}

/**
 * 上传附件
 */
function uploadFiles() {
    var obj_editor = card_id = null;
    // $('.btn-fileupload').fileupload({
    //     dataType: 'json',
    //     url: '/archiveadmin/content/uploadFile',
    //     submit: function(e, data) {
    //         /* 获得当前操作的资料卡上传容器对象 */
    //         obj_editor = $(this).parent().parent().parent();
    //         card_id = obj_editor.data('card_id');
    //
    //         /* 上传按钮样式处理 */
    //         obj_editor.find('.fileinput-button').attr('disabled', true);
    //         obj_editor.find('.fileinput-button').find('.text').hide();
    //         obj_editor.find('.fileinput-button').find('.text-active').removeClass('text-active');
    //
    //         /* 等待状态处理 */
    //         obj_editor.find('.spin-notice').addClass('show inline');
    //     },
    //     done: function (e, data) {
    //         /* 等待状态处理 */
    //         obj_editor.find(".spin-notice").removeClass('show inline');
    //
    //         /* 上传按钮样式处理 */
    //         obj_editor.find('.fileinput-button').removeAttr('disabled');
    //         obj_editor.find('.fileinput-button').find('.text').show();
    //         obj_editor.find('.fileinput-button').find('span').eq(1).addClass('text-active');
    //
    //         /* 返回数据显示处理 */
    //         if (data.result.status == 0) {
    //             showFile(data.result.data, obj_editor, card_id);
    //         }
    //     }
    // });
}

/**
 * 将上传的附件以列表形式显示出来
 */
function showFile(d, obj_editor, card_id, trObj) {
    /* 处理展示列表的显示状态 */
    if (d.mediatype == 'image') {
        obj_editor.find('.gallery-image').show();
        showFileImage(d, obj_editor, card_id);
    } else if (d.mediatype == 'audio') {
        obj_editor.find('.gallery-audio').show();
        showFileAudio(d, obj_editor, card_id);
    } else if (d.mediatype == 'video') {
        obj_editor.find('.gallery-video').show();
        showFileVideo(d, obj_editor, card_id);
    } else {
        obj_editor.find('.gallery-image').show();
        showFileImage(d, obj_editor, card_id);
    }
}

function replaceShowFile(d, obj, obj_editor, card_id) {
    if (d.mediatype == 'image') {
        obj_editor.find('.gallery-image').show();
        replaceFileImageList(d, obj, card_id);
    } else if (d.mediatype == 'audio') {
        obj_editor.find('.gallery-audio').show();
        replaceFileAudio(d, obj, obj_editor, card_id);
    } else if (d.mediatype == 'video') {
        obj_editor.find('.gallery-video').show();
        replaceFileVideo(d, obj, obj_editor, card_id);
    } else {
        obj_editor.find('.gallery-image').show();
        replaceFileImageList(d, obj, card_id);
    }
}

function replaceFileImageList(d, obj, card_id) {
    let image = '';
    let edit_btn = '';
    let delete_btn = '';
    if (d.url) {
        image = '<a href="/workbenchadmin/image?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' +
            '<img src="' + d.url + '" class="thumb-lg" border="0" />' +
            '</a>';
        edit_btn = '<a href="/workbenchadmin/image?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">';
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="img" data-duration="0">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
    } else {
        image = '<a href="javascript:;" class="btn btn-xs edit-single">' +
            '<i class="fa fa-picture-o text-xxxl"></i> </a>';
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled" >';
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="img" data-duration="0">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
    }
    var tr_str = '<td class="v-middle hide"><input type="checkbox" class="select-single" value="' + d._id + '" /></td>' +
        '<td class="v-middle">' + image +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        '<i class="fa fa-pencil"></i><span> 编辑</span>' +
        '</a> ' +
        delete_btn +
        '</td>'

    obj.html(tr_str);
}

/**
 * 将上传的附件以列表形式显示出来 - 图片类型
 */
function showFileImage(d, obj_editor, card_id) {
    let image = '';
    let edit_btn = '';
    let delete_btn = '';
    if (d.url) {
        image = '<a href="/workbenchadmin/image?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' +
            '<img src="' + d.url + '" class="thumb-lg" border="0" />' +
            '</a>';
        edit_btn = '<a href="/workbenchadmin/image?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">'
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="img" data-duration="0">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>'
    } else {
        image = '<a href="javascript:;" class="btn  btn-xs edit-single">' +
            '<i class="fa fa-picture-o text-xxxl"></i> </a>'
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled" >'
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="img" data-duration="0">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>'
    }
    var tr_str = '<tr attachment_id="' + d._id + '">' +
        '<td class="v-middle hide"><input type="checkbox" class="select-single" value="' + d._id + '" /></td>' +
        '<td class="v-middle">' + image +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        '<i class="fa fa-pencil"></i><span> 编辑</span>' +
        '</a> ' +
        delete_btn +
        '</td>' +
        '</tr>';
    obj_editor.find(".table-striped-image>tbody").append(tr_str);

    /* 上传附件 ID 处理 */
    var obj_attachment_ids = obj_editor.parent().find("input[name='attachments_img[]']:last");
    var attachment_ids = obj_attachment_ids.val();
    if (!attachment_ids) {
        obj_attachment_ids.val(d._id);
    } else {
        obj_attachment_ids.val(attachment_ids + ',' + d._id);
    }

    /* 处理附件数 */
    obj_editor.find('.gallery-image').find('.count-attachment').text(obj_editor.find('.table-striped-image>tbody>tr').size());
}

function replaceFileAudio(d, obj, obj_editor, card_id) {
    let checkbox = '';
    let cover = '';
    let edit_btn = '';
    let delete_btn = '';
    let duration = '';
    if (d.extra && d.extra.duration) {
        checkbox = '<input type="checkbox" class="select-single" value="' + d._id + '" />';

        cover = '<a href="/workbenchadmin/audio?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' +
            '<i class="fa fa-music text-xxxl"></i>' +
            '</a>';

        edit_btn = '<a href="/workbenchadmin/audio?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="audio" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration = '<td class="v-middle">' + d.extra.duration + ' 秒</td>'
    } else {
        checkbox = '<input type="checkbox" class="select-single" value="' + d._id + '" disabled />';
        cover = '<a href="javascript:;"  >' +
            '<i class="fa fa-music text-xxxl"></i>' +
            '</a>'
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="audio" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration = '<td class="v-middle">--</td>'
    }

    var tr_str = '<td class="v-middle hide">' + checkbox + '</td>' +
        '<td class="v-middle">' +
        cover +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        duration +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        delete_btn +
        '</td>';
    obj.html(tr_str)

    /* 处理总时长 */
    var obj_duration = obj_editor.parent().find("input[name='duration_audio[]']:last");
    var count_duration = parseInt(obj_duration.val() ? obj_duration.val() : 0) + parseInt(d.extra && d.extra.duration ? d.extra.duration : 0);
    obj_duration.val(count_duration);
    obj_editor.find('.gallery-audio').find('.count-duration').text(count_duration);
}

/**
 * 将上传的附件以列表形式显示出来 - 音频类型
 */
function showFileAudio(d, obj_editor, card_id) {
    let checkbox = '';
    let cover = '';
    let edit_btn = '';
    let delete_btn = '';
    let duration = '';
    if (d.extra && d.extra.duration) {
        checkbox = '<input type="checkbox" class="select-single" value="' + d._id + '" />';

        cover = '<a href="/workbenchadmin/audio?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">' +
            '<i class="fa fa-music text-xxxl"></i>' +
            '</a>';

        edit_btn = '<a href="/workbenchadmin/audio?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="audio" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration = '<td class="v-middle">' + d.extra.duration + ' 秒</td>'
    } else {
        checkbox = '<input type="checkbox" class="select-single" value="' + d._id + '" disabled />';
        cover = '<a href="javascript:;"  >' +
            '<i class="fa fa-music text-xxxl"></i>' +
            '</a>'
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="audio" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration = '<td class="v-middle">--</td>'
    }

    var tr_str = '<tr attachment_id="' + d._id + '">' +
        '<td class="v-middle hide">' + checkbox + '</td>' +
        '<td class="v-middle">' +
        cover +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        duration +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        delete_btn +
        '</td>' +
        '</tr>';
    obj_editor.find(".table-striped-audio>tbody").append(tr_str);

    /* 上传附件 ID 处理 */
    var obj_attachment_ids = obj_editor.parent().find("input[name='attachments_audio[]']:last");
    var attachment_ids = obj_attachment_ids.val();
    if (!attachment_ids) {
        obj_attachment_ids.val(d._id);
    } else {
        obj_attachment_ids.val(attachment_ids + ',' + d._id);
    }

    /* 处理总时长 */
    var obj_duration = obj_editor.parent().find("input[name='duration_audio[]']:last");
    var count_duration = parseInt(obj_duration.val() ? obj_duration.val() : 0) + parseInt(d.extra && d.extra.duration ? d.extra.duration : 0);
    obj_duration.val(count_duration);
    obj_editor.find('.gallery-audio').find('.count-duration').text(count_duration);

    /* 处理附件数 */
    obj_editor.find('.gallery-audio').find('.count-attachment').text(obj_editor.find('.table-striped-audio>tbody>tr').size());
}

function replaceFileVideo(d, obj, obj_editor, card_id) {
    /* 检查该文件是否已在缓存池 */
    if (new_uploaded_files.in_array(d._id)) {
        return false;
    }

    let edit_cover = '';
    let edit_btn = '';
    let delete_btn = '';
    let duration = '';

    if (d.extra && d.extra.duration) {
        edit_cover = '<a href="/workbenchadmin/vod?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">'
        edit_btn = '<a href="/workbenchadmin/vod?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="video" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>'

        duration =  '<td class="v-middle">' + d.extra.duration + ' 秒</td>'
    } else {
        edit_cover = '<a href="javacript:;" title="点击进入编辑" >'
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="video" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration =  '<td class="v-middle">--</td>'
    }

    var tr_str = '<td class="v-middle hide"><input type="checkbox" class="select-single" value="' + d._id + '" /></td>' +
        '<td class="v-middle">' +
        edit_cover;

    if (d.extra.cover) {
        tr_str += '<img src="' + d.extra.cover + '" class="thumb-lg" border="0" />';
    } else {
        tr_str += '<i class="fa fa-film text-xxxl"></i>';
    }

    tr_str += '</a>' +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        duration +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        delete_btn +
        '</td>';
    obj.html(tr_str)

    /* 处理总时长 */
    var obj_duration = obj_editor.parent().find("input[name='duration_video[]']:last");
    var count_duration = parseInt(obj_duration.val() ? obj_duration.val() : 0) + parseInt(d.extra && d.extra.duration ? d.extra.duration : 0);
    obj_duration.val(count_duration);
    obj_editor.find('.gallery-video').find('.count-duration').text(count_duration);

    /* 将以上处理过的附件ID记入缓存池 */
    if (d.extra && d.extra.duration) {
        new_uploaded_files.push(d._id);
    }
}

/**
 * 将上传的附件以列表形式显示出来 - 视频类型
 */
function showFileVideo(d, obj_editor, card_id) {
    let edit_cover = '';
    let edit_btn = '';
    let delete_btn = '';
    let duration = '';
    if (d.extra && d.extra.duration) {
        edit_cover = '<a href="/workbenchadmin/vod?_id=' + d._id + '&card_id=' + card_id + '" title="点击进入编辑" target="_blank">'
        edit_btn = '<a href="/workbenchadmin/vod?_id=' + d._id + '&card_id=' + card_id + '" class="btn btn-primary btn-xs edit-single" target="_blank">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs delete-single" data-delete-type="video" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>'

        duration =  '<td class="v-middle">' + d.extra.duration + ' 秒</td>'
    } else {
        edit_cover = '<a href="javacript:;" title="点击进入编辑" >'
        edit_btn = '<a href="javascript:;" class="btn btn-primary btn-xs edit-single disabled">' +
            '<i class="fa fa-pencil"></i><span> 编辑</span>' +
            '</a> '
        delete_btn = '<button type="button" class="btn btn-danger btn-xs disabled" disabled data-delete-type="video" data-duration="' + d.extra.duration + '">' +
            '<i class="fa fa-trash-o"></i>' +
            '<span> 删除</span>' +
            '</button>';
        duration =  '<td class="v-middle">--</td>'
    }

    var tr_str = '<tr attachment_id="' + d._id + '">' +
        '<td class="v-middle hide"><input type="checkbox" class="select-single" value="' + d._id + '" /></td>' +
        '<td class="v-middle">' +
        edit_cover;

    if (d.extra.cover) {
        tr_str += '<img src="' + d.extra.cover + '" class="thumb-lg" border="0" />';
    } else {
        tr_str += '<i class="fa fa-film text-xxxl"></i>';
    }

    tr_str += '</a>' +
        '</td>' +
        '<td class="v-middle">' + d.title + '</td>' +
        duration +
        '<td class="v-middle">' + d.filesize_format + '</td>' +
        '<td class="v-middle">' + d.ctime + '</td>' +
        '<td class="v-middle">' +
        edit_btn +
        delete_btn +
        '</td>' +
        '</tr>';
    obj_editor.find(".table-striped-video>tbody").append(tr_str);

    /* 上传附件 ID 处理 */
    var obj_attachment_ids = obj_editor.parent().find("input[name='attachments_video[]']:last");
    var attachment_ids = obj_attachment_ids.val();
    if (!attachment_ids) {
        obj_attachment_ids.val(d._id);
    } else {
        obj_attachment_ids.val(attachment_ids + ',' + d._id);
    }

    /* 处理附件数 */
    obj_editor.find('.gallery-video').find('.count-attachment').text(obj_editor.find('.table-striped-video>tbody>tr').size());
}
